﻿/*
 * @author Lee 
 * Time: 20180403 
 * Abstract: SharePoint API的二次封装
 */

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
using System;
using System.Collections.Specialized;
using System.Net.Mail;

namespace SharePointHelper.DLL.SSOM
{
    public class Email
    {
        #region 发送email
        /// <summary>
        /// 发送email
        /// </summary>
        /// <param name="messageHeaders">
        /// 例如  messageHeaders.Add("to", "1037949156@qq.com");
        ///       messageHeaders.Add("from", "v-leeliu@microsoft.com");
        ///       messageHeaders.Add("subject", "your subject");
        ///       messageHeaders.Add("content-type", "text/html");
        /// </param>
        /// <returns></returns>
        public static bool SendEmail(StringDictionary messageHeaders,string emailBody, SPWeb currentWeb)
        {
            bool IsSent = false;           
            try
            {
                IsSent = SPUtility.SendEmail(currentWeb, messageHeaders, emailBody);
                
            }
            catch (Exception ex)
            {
                //Exception handling
            }
            return IsSent;
        }
        

        public static void sendEmail()
        {
            SPWebApplication webApp = SPWebApplication.Lookup(new Uri("https://serverName/"));
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("abc@xyz.com");
            mail.To.Add("v-leeliu@microsoft.com");          
            mail.Subject = "Subject Test";
            mail.Body = "Body Test";
            // SmtpClient class sends the email by using the specified SMTP server
            SmtpClient smtp = new SmtpClient(webApp.OutboundMailServiceInstance.Server.Address);
            smtp.UseDefaultCredentials = true;
            smtp.Send(mail);
        }
        #endregion
    }
}

